package org.weso.acota.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.weso.acota.services.to.FeedResourceTO;
import org.weso.acota.services.to.ListFeedResourceTO;
import org.weso.acota.services.to.ListFeedTagsTO;
import org.weso.acota.to.feedback.FeedbackResource;
import org.weso.acota.to.feedback.FeedbackTag;
import org.weso.acota.to.feedback.TagType;

@Path("/feedback")
public class AcotaFeedbackRest {
	
	private AcotaFeedbackServiceFacade acotaFeedback;
	
	public AcotaFeedbackRest(){
		this.acotaFeedback = new AcotaFeedBackImpl();
	}

	public TagType setType(String type){		
		if(type.endsWith("SELECTED"))
			return TagType.SELECTED;
		if(type.endsWith("PROVIDED"))
			return TagType.PROVIDED;
		if(type.endsWith("REJECTED"))
			return TagType.REJECTED;
		else
			return TagType.PROVIDED;
	}
		
	@GET 
	@Path("saveSuggestion")
    @Produces({"text/plain", "application/xml", "application/json"})
    public FeedResourceTO saveSuggestion(
    		@QueryParam("title") String title,
    		@QueryParam("description") String description,
    		@QueryParam("uri") String uri,
    		@QueryParam("tags") String tags, /*Format of list of tag label@@weigth@@type;*/
    		@QueryParam("activity") String activity,
    		@QueryParam("user") String user	
    ){
		String docUri="";
		try{
			List<FeedbackTag> feedTags = new ArrayList<FeedbackTag>();
			for(int i=0; i< tags.split(";").length; i++){
				FeedbackTag tag = new FeedbackTag();
				tag.setLabel(tags.split(";")[i].split("@@")[0]);
				tag.setWeight(Integer.parseInt(tags.split(";")[i].split("@@")[1]));
				tag.setTagType(setType(tags.split(";")[i].split("@@")[2]));
				feedTags.add(tag);
			}
			Date taggingDate = new Date();
			docUri = acotaFeedback.saveSuggestions(user, activity, taggingDate, uri, title, description, feedTags);
			FeedResourceTO resultSave = new FeedResourceTO();
			resultSave.setDocUri(docUri);
			resultSave.setResource(new FeedbackResource(title, description, uri, activity, feedTags));
			return resultSave;
		}catch (Exception e) {
			throw new WebApplicationException(Response.Status.BAD_REQUEST);
		}
	}
	
//	@GET
//	@Path("savePost")
//    @Produces({"text/plain", "application/xml", "application/json"})
//    public FeedResourceTO savePost(
//    		@QueryParam("title") String title,
//    		@QueryParam("description") String description,
//    		@QueryParam("uri") String uri,
//    		@QueryParam("tagsType") String[] tagsType,
//    		@QueryParam("tags") String[] tags,
//    		@QueryParam("values") String[] values,
//    		@QueryParam("activity") String activity,
//    		@QueryParam("user") String user	
//    ){
////		for(int i=0; i< tags.length; i++){
////			System.out.println("*******************************"+tags[i]);
////		}
////			System.out.println("*******************************"+title);
////		try{
////			FeedResourceTO resultSave = new FeedResourceTO();
////			return resultSave;
////		}catch (Exception e) {
////			throw new WebApplicationException(Response.Status.BAD_REQUEST);
////		}
//		
//		return null;
//	}
	
	@GET 
	@Path("DocumentByTag")
    @Produces({"text/plain", "application/xml", "application/json"})
    public ListFeedResourceTO DocumentByTag(@QueryParam("tag") String tag){
		try{
			ListFeedResourceTO documents = new ListFeedResourceTO();
			documents.setFeedbackresources(acotaFeedback.getTaggedDocuments(tag));
			return documents;
		}catch (Exception e) {
			throw new WebApplicationException(Response.Status.BAD_REQUEST);
		}
	}
	
	@GET
    @Path("tagByDocument")
    @Produces({"text/plain", "application/xml", "application/json"})
    public ListFeedTagsTO tagByDocument(@QueryParam("document") String document){
		try{
			ListFeedTagsTO tags = new ListFeedTagsTO();
			tags.setFeedbacktags(acotaFeedback.getDocumentTags(document));
			return tags;
		}catch (Exception e) {
			throw new WebApplicationException(Response.Status.BAD_REQUEST);
		}
	}
	
	@GET
    @Path("tagByUser")
    @Produces({"text/plain", "application/xml", "application/json"})
    public ListFeedTagsTO tagByUser(@QueryParam("user") String username){
		try{
			ListFeedTagsTO tags = new ListFeedTagsTO();
			tags.setFeedbacktags(acotaFeedback.getUserTags(username));
			return tags;
		}catch (Exception e) {
			throw new WebApplicationException(Response.Status.BAD_REQUEST);
		}
	}

}
